/*
 * Copyright (c) 2015 DHC Corporation.
 */
package cn.com.dhc.danlu.util;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

/**
 * 存储基类
 *
 * @author MuWei
 * @version 1.00 2015/02/03 新建
 */
public class PreferenceUtils {
    /**
     * int类型初期值
     */
    public static final int INT_NONE = -1;
    /**
     * boolean类型初期值
     */
    public static final boolean BOOLEAN_NONE = false;
    /**
     * String类型初期值
     */
    private static final String STRING_NONE = "";
    /**
     * version版本号
     */
    private static String VERSION = "version";
    /**
     * userid
     */
    private static String USERID = "userid";
    /**
     * fullname
     */
    private static String FULLNAME = "fullname";
    /**
     * 更新版本信息
     */
    private static String UPDATE_VERSION = "updateVersion";
    /**
     * 是否弹出过
     */
    private static String LOGIN_NAME_POP = "isPop";
    /**
     * 用户名key
     */
    private static String LOGIN_NAME = "loginName";
    /**
     * 密码key
     */
    private static String PASSWORD = "password";
    /**
     * SharedPreferences
     */
    private SharedPreferences settingPref;
    /**
     * Editor
     */
    private Editor settingEditor;

    public PreferenceUtils (Context context) {
        this (context, CommonConst.DAN_LU);
    }

    @SuppressLint ("CommitPrefEdits")
    public PreferenceUtils (Context context, String fileName) {
        settingPref = context.getSharedPreferences (fileName, Context.MODE_PRIVATE);
        settingEditor = settingPref.edit ();
    }

    /**
     * 清空SharedPreferences
     */
    public void clear () {
        settingEditor.clear ();
        settingEditor.commit ();
    }

    /**
     * 保存用户名与密码。
     *
     * @param loginName
     *         用户名
     * @param password
     *         加密后的密码
     */
    public void setLoginNameAndPassword (String loginName, String password) {
        settingEditor.putString (LOGIN_NAME, loginName);
        settingEditor.putString (PASSWORD, password);
        settingEditor.commit ();
    }

    /**
     * 获取用户名
     *
     * @return 保存的用户名
     */
    public String getLoginName () {
        return settingPref.getString (LOGIN_NAME, STRING_NONE);
    }

    /**
     * 保存用户名
     *
     * @param loginName
     *         用户名
     */
    public void setLoginName (String loginName) {
        settingEditor.putString (LOGIN_NAME, loginName);
        settingEditor.commit ();
    }

    /**
     * 获取密码
     *
     * @return 加密后的密码
     */
    public String getPassword () {
        return settingPref.getString (PASSWORD, STRING_NONE);
    }

    /**
     * 保存密码
     *
     * @param password
     *         加密后的密码
     */
    public void setPassword (String password) {
        settingEditor.putString (PASSWORD, password);
        settingEditor.commit ();
    }

    /**
     * 清理用户名与密码
     */
    public void clearLoginNameAndPassword () {
        settingEditor.remove (LOGIN_NAME);
        settingEditor.remove (PASSWORD);
        settingEditor.commit ();
    }

    /**
     * 清理用户名
     */
    public void clearLoginName () {
        settingEditor.remove (LOGIN_NAME);
        settingEditor.commit ();
    }

    /**
     * 清理密码
     */
    public void clearPassword () {
        settingEditor.remove (PASSWORD);
        settingEditor.commit ();
    }

    /**
     * 获取版本信息
     */
    public String getVersion () {
        return settingPref.getString (VERSION, STRING_NONE);
    }

    /**
     * 设置版本信息
     */
    public void setVersion (String version) {
        settingEditor.putString (VERSION, version);
        settingEditor.commit ();
    }

    /**
     * 清空版本号
     */
    public void clearVersion () {
        settingEditor.remove (VERSION);
        settingEditor.commit ();
    }

    /**
     * 获取是否弹出
     */
    public String getIsPop () {
        return settingPref.getString (LOGIN_NAME_POP, STRING_NONE);
    }

    /**
     * 设置是否弹出过对话框
     */
    public void setIsPop (String isPop) {
        settingEditor.putString (LOGIN_NAME_POP, isPop);
        settingEditor.commit ();
    }

    /**
     * 获取更新版本信息
     */
    public String getUpdateVersion () {
        return settingPref.getString (UPDATE_VERSION, STRING_NONE);
    }

    /**
     * 设置更新版本信息
     */
    public void setUpdateVersion (String updateVersion) {
        settingEditor.putString (UPDATE_VERSION, updateVersion);
        settingEditor.commit ();
    }

    /**
     * 获取userid
     */
    public String getUserId () {
        return settingPref.getString (USERID, STRING_NONE);
    }

    /**
     * 设置userid
     */
    public void setUserId (String userid) {
        settingEditor.putString (USERID, userid);
        settingEditor.commit ();
    }

    /**
     * 清空userid
     */
    public void clearUserId () {
        settingEditor.remove (VERSION);
        settingEditor.commit ();
    }

    /**
     * 获取fullname
     */
    public String getFullName () {
        return settingPref.getString (FULLNAME, STRING_NONE);
    }

    /**
     * 设置fullname
     */
    public void setFullName (String fullName) {
        settingEditor.putString (FULLNAME, fullName);
        settingEditor.commit ();
    }
}